package com.scisoft.scan.dy;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
import android.widget.Toast;

import com.scisoft.scan.control.ScanHelper;
import com.scisoft.scan.dy.R;


public class TestScanActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_scan);
        //初始化
        init();
    }

    //初始化
    private void init(){
        //初始化Profile
        ScanHelper.CreateProfile(this);

        //设置广播消息的接收器
        registerReceivers();

        //接收状态变化消息
        //ScanHelper.registerForScannerStatus(this);

        //接收版本信息
        //ScanHelper.registerForVersionInfo(this);
    }

    // 设置广播消息的接收器
    private void registerReceivers() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ScanHelper.ACTION_RESULT_NOTIFICATION);   // for notification result
        filter.addAction(ScanHelper.ACTION_RESULT);                // for error code result
        filter.addCategory(Intent.CATEGORY_DEFAULT);    // needed to get version info
        filter.addAction(ScanHelper.ACTION_SCAN_OUTPUT); // register to received broadcasts via DataWedge scanning
        registerReceiver(myBroadcastReceiver, filter);
    }

    private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ScanHelper.ACTION_SCAN_OUTPUT))
            {
                // 处理扫码结果
                try
                {
                    String decodedData = intent.getStringExtra("com.symbol.datawedge.data_string");
                    TextView textView = findViewById(R.id.et_numberinput_data);
                    textView.setText(decodedData);
                }
                catch (Exception e)
                {
                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }

        }
    };

    @Override
    protected void onResume()
    {
        super.onResume();
        registerReceivers();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        unregisterReceiver(myBroadcastReceiver);
        //ScanHelper.unRegisterScannerStatus(this);
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
    }

    @Override
    protected void onStart()
    {
        super.onStart();
    }

    @Override
    protected void onStop()
    {
        super.onStop();
    }

}
